﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Design;
using DevExpress.XtraGrid.Columns;
using ProgNetComponents.ExtenderProviders;
using DevExpress.XtraGrid.Views.Base;
using ProgNetDxComponents.Design.Creators;

namespace ProgNetDxComponents.Design
{
    internal class DxGridColumnExtenderDesigner : System.ComponentModel.Design.ComponentDesigner
    {
        public override DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerbCollection v = new DesignerVerbCollection();
                v.Add(new DesignerVerb("Create Column Repositories", CreateColumnRepositories));
                return v;
            }
        }
        protected List<GridColumn> GetAllGridColumns()
        {
            List<GridColumn> columns = new List<GridColumn>();
            if (this.Component != null && Component is ProgNetComponents.ExtenderProviders.BaseExtenderProvider)
            {
                BaseExtenderProvider ex = Component as ProgNetComponents.ExtenderProviders.BaseExtenderProvider;
                IEnumerable<object> all = ex.GetAllKeys();
                foreach (object o in all)
                {
                    if (o is ColumnView)
                    {
                        foreach (GridColumn c in (o as ColumnView).Columns)
                        {
                            if (!columns.Contains(c))
                            {
                                columns.Add(c);
                            }
                        }
                    }
                    else if (o is GridColumn)
                    {
                        GridColumn c = o as GridColumn;
                        if (c.View != null && !columns.Contains(c))
                        {
                            columns.Add(c);
                        }
                    }
                }
            }
            return columns;
        }

        private void CreateColumnRepositories(object sender, EventArgs e)
        {
            List<GridColumn> columns = GetAllGridColumns();
            
            if (columns.Count > 0)
            {
                IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
                GridColumnRepositoryCreator creator = new GridColumnRepositoryCreator(columns, host);
                creator.ShowDialog();
                creator.Dispose();
            }
        }
    }
}
